<?php

/**
 * 前台用户登录、注册控制器
 */
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Support\Facades\Auth;

use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index(){
		dd(Auth::user());
        return '用户首页';
    }
	
	//前台登录
	public function login(Request $request){
		if (Auth::check()) {
            return redirect('/user');//已登录跳转
        }
		//echo url()->previous();
		if($request->isMethod('post')){
			//captcha: "123"password: "123456"email: "test11@qq.com"_token: "biPZt1sBUXCO28vPnmV7rQCwotYMd1VEcoz84pJI"
            $query = $request->input();
			
			//验证
			$validator = Validator::make($query, 
			 [//rules
				 //'name' => 'required',
				 'email' => 'required|email',
				 'password' => 'required|min:8',
				 //'captcha' => 'required|captcha',
			 ], 
			 [//messages
				//'name.required' => '姓名不能为空',
				'email.required' => '邮箱不能为空',
				'email.email' => '请输入有效的邮箱地址',
				'password.required' => '密码不能为空',
				'password.min' => '密码长度不能少于8个字符',
				//'captcha.required' => '验证码不能为空',
				//'captcha.captcha' => '验证码不正确',
			 ]);
			 if ($validator->fails()) {
				return array('error' => 0, 'info' => $validator->errors()->first());
			 }
			 
			 if (!captcha_check($query['captcha'])) {
				 return array('error' => 0, 'info' => '验证码不正确');
			 }
			 
            //用户认证
            if (Auth::attempt(['email' => $query['email'], 'password' => $query['password']])) {
                // 认证成功
                return array('error' => 1, 'info' => '登录成功');
            } else {
                // 认证失败
                return array('error' => 0, 'info' => '登录失败','data'=>$query);
            }
        }
		
        return view('front/user/login');
    }
	
	//前台注册
	public function register(Request $request){
		if (Auth::check()) {
            return redirect('/home');//已登录跳转
        }
		
		if($request->isMethod('post')){
            $query = request()->input();
			
			//验证
			$validator = Validator::make($query, 
			 [//rules
				 'name' => 'required',
				 'email' => 'required|email',
				 'password' => 'required|min:8',
				 //'captcha' => 'required|captcha',
			 ], 
			 [//messages
				'name.required' => '姓名不能为空',
				'email.required' => '邮箱不能为空',
				'email.email' => '请输入有效的邮箱地址',
				'password.required' => '密码不能为空',
				'password.min' => '密码长度不能少于8个字符',
				//'captcha.required' => '验证码不能为空',
				//'captcha.captcha' => '验证码不正确',
			 ]);
			 if ($validator->fails()) {
				//dd($validator->errors());
				//return response()->json(['error'=>0,'info' => $validator->errors()], 400);
				return array('error' => 0, 'info' => $validator->errors()->first());
			 }

            //简单验
            if ($query['confirmPassword'] != $query['password']) {
                return array('error' => 0, 'info' => '两次密码不一样');
            }
			if (!captcha_check($query['captcha'])) {
				 return array('error' => 0, 'info' => '图形验证码不正确');
			}
            //手机验证码
            //$verifyData = \Cache::get($query['vercodekey']);
            //if (!$verifyData) {
            //    return array('error' => 0, 'info' => '验证码已失效');
            //}
            //if (!hash_equals($verifyData['code'], $query['vercode'])) {
            //    return array('error' => 0, 'info' => '验证码错误');
            //}

			
            //agreement: "on"captcha: "htxu"confirmPassword: "12345678"email: "test1@qq.com"name: "test1"password: "12345678"_token: "huYbudTnoCj86O7H0uj5apK27hGULcbS5vXEVCwM"
            //入库
            $user = User::create([
                'name' => $query['name'],
                'email' => $query['email'],
                //'phone' => $verifyData['phone'],
                'password' => bcrypt($query['password']),
            ]);

            //用户认证
            if (Auth::attempt(['email' => $query['email'], 'password' => $query['password']])) {
                // 认证成功
                return array('error' => 1, 'info' => '登录成功');
            } else {
                // 认证失败
                return array('error' => 0, 'info' => '登录失败');
            }
		}
		return view('front/user/register');
	}
	
	/**
	 * 将用户退出应用程序。
	 */
	public function logout(Request $request){
		Auth::logout();
		$request->session()->invalidate();
		$request->session()->regenerateToken();
		return redirect('/login');
	}

}
